﻿using LPLS.Commons.Enums;
using LPLS.DBEntities.OEEModule.OEEModels.TimeUtilizationModels;
using LPLS.DBInterface.OEEModule.OEEModels.TimeUtilizationModels;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace DataStatisticsChartModule.ViewModels
{
    public class TimeUtilizationRateConfigViewModel : BindableBase
    {

        private readonly ITimeUtilizationRateConfigService _timeUtilizationRateConfigService;


        public TimeUtilizationRateConfigViewModel(ITimeUtilizationRateConfigService timeUtilizationRateConfigService)
        {
            LoadCompentTypeList();
            _timeUtilizationRateConfigService = timeUtilizationRateConfigService;
            DataStatisticsEvent.OnDataStatisticsEvent += DataStatisticsEvent_OnDataStatisticsEvent;
            SaveCommand = new DelegateCommand(SaveData);

        }


        private DataStatisticsEventModel eventModel = new DataStatisticsEventModel();
        private void DataStatisticsEvent_OnDataStatisticsEvent(DataStatisticsEventModel model)
        {
            eventModel = model;

        }

        private ObservableCollection<TimeStatus> _LoadingTimeList;
        /// <summary>
        /// 负荷时间
        /// </summary>
        public ObservableCollection<TimeStatus> LoadingTimeList
        {
            get { return _LoadingTimeList; }
            set { SetProperty(ref _LoadingTimeList, value); }
        }



        private ObservableCollection<TimeStatus> _StopTimeList;
        /// <summary>
        /// 停机时间
        /// </summary>
        public ObservableCollection<TimeStatus> StopTimeList
        {
            get { return _StopTimeList; }
            set { SetProperty(ref _StopTimeList, value); }
        }


        private void LoadEnumData()
        {

            LoadingTimeList = new ObservableCollection<TimeStatus>();
            StopTimeList = new ObservableCollection<TimeStatus>();
            ///获取到数据库数据
            var data = _timeUtilizationRateConfigService.GetData(eventModel.DeviceModel.ID, SelectCompentType);

            var (StatusenumValues, enumValueInts, descriptions) = StatusEnumHelper.GetEnumCollections();

            foreach (var item in StatusenumValues)
            {


                var loadTimemModel = data.Where(e => e.Status == item && e.Type == 1).ToList()?.FirstOrDefault();

                if (loadTimemModel != null)
                {
                    TimeStatus model = new TimeStatus();
                    model.Status = StatusEnumHelper.GetDescription(item);
                    model.MyStatusEnum = item;
                    model.IsCheck = true;
                    LoadingTimeList.Add(model);
                }
                else
                {
                    TimeStatus model = new TimeStatus();
                    model.Status = StatusEnumHelper.GetDescription(item);
                    model.MyStatusEnum = item;
                    model.IsCheck = false;
                    LoadingTimeList.Add(model);
                }

                var stopTimeModel = data.Where(e => e.Status == item && e.Type == 2).ToList()?.FirstOrDefault();

                if (stopTimeModel != null)
                {
                    TimeStatus model = new TimeStatus();
                    model.Status = StatusEnumHelper.GetDescription(item);
                    model.MyStatusEnum = item;
                    model.IsCheck = true;
                    StopTimeList.Add(model);
                }
                else
                {

                    TimeStatus model = new TimeStatus();
                    model.Status = StatusEnumHelper.GetDescription(item);
                    model.MyStatusEnum = item;
                    model.IsCheck = false;
                    StopTimeList.Add(model);
                }


            }



        }


        private string _CompentTypeName;
        public string CompentTypeName
        {
            get { return _CompentTypeName; }
            set
            {

                SetProperty(ref _CompentTypeName, value);
                if (value != null)
                {
                    SelectCompentType = DeviceComponentEnumExtension.GetEnumByDescription(value);
                }
            }
        }

        private DeviceComponentTypeEnum _SelectCompentType;
        public DeviceComponentTypeEnum SelectCompentType
        {
            get { return _SelectCompentType; }
            set
            {

                SetProperty(ref _SelectCompentType, value);
                if (value != null)
                {
                    LoadEnumData();
                }


            }
        }

        private ObservableCollection<string> _CompentTypeList;
        public ObservableCollection<string> CompentTypeList
        {
            get { return _CompentTypeList; }
            set { SetProperty(ref _CompentTypeList, value); }
        }

        private void LoadCompentTypeList()
        {


            var data = DeviceComponentEnumExtension.GetAllDescriptions();

            CompentTypeList = new ObservableCollection<string>(data);







        }

        private void SaveData()
        {
            List<TimeUtilizationRateConfig> li = new List<TimeUtilizationRateConfig>();
            foreach (var item in StopTimeList)
            {

                if (item.IsCheck)
                {
                    TimeUtilizationRateConfig model = new TimeUtilizationRateConfig();
                    model.Id = Guid.NewGuid();
                    model.Status = item.MyStatusEnum;
                    model.CompentType = SelectCompentType;
                    model.Type = 2;
                    model.DeviceID = eventModel.DeviceModel.ID;
                    li.Add(model);

                }

            }
            _timeUtilizationRateConfigService.AddData(li, eventModel.DeviceModel.ID,2,this.SelectCompentType);

            li.Clear();
            foreach (var item in LoadingTimeList)
            {
                if (item.IsCheck)
                {
                    TimeUtilizationRateConfig model = new TimeUtilizationRateConfig();
                    model.Id = Guid.NewGuid();
                    model.Status = item.MyStatusEnum;
                    model.CompentType = SelectCompentType;
                    model.Type = 1;
                    model.DeviceID = eventModel.DeviceModel.ID;
                    li.Add(model);

                }

            }
            _timeUtilizationRateConfigService.AddData(li, eventModel.DeviceModel.ID, 1,this.SelectCompentType);


        }

        public DelegateCommand SaveCommand { get; }

    }







    public class TimeStatus
    {

        public StatusEnum MyStatusEnum { get; set; }

        public string Status { get; set; }

        public bool IsCheck { get; set; }
    }
}
